var _oFormXml;
var _oPropertiesXml;
var _oFieldsXml;

var _oEvents;
var _oEvent;
var _oDependencies;


var _oXmlDoc = CreateXmlDocument();

var _oXslDoc = CreateXmlDocument(true);

var _oXslt = CreateXslTemplate();

var _oXslProc;

function ShowTabSections(bPreviewMode)
{
ShowTabSection("ColumnLayout");
ShowTabSection("RowLayout");
if (!bPreviewMode)
{
ShowTabSection("Location");
}
}

function ShowTabSection(sSectionName)
{
var oSection = document.all["div" + sSectionName + "Section"];
if (oSection != null)
{
oSection.style.display = "inline";
}
}

function UpdateSections()
{
var oSections = _oFormXml.selectNodes("/entity/form/tabs/tab[@name = '" + crmDialog.TabName.value + "']/sections/section[not(@locklevel) or @locklevel = '0']");

var SelectSections		= new Select();
SelectSections.ID		= "SectionName";
SelectSections.OnChange	= "UpdateLayout(this)";
SelectSections.Selected = oSections[0].getAttribute("name");

var i;
var iLen = oSections.length;

for (i = 0; i < iLen; i++)
{
SelectSections.AddOption(oSections[i].selectSingleNode("labels/label[@languagecode = " + USER_LANGUAGE_CODE + "]/@description").text, oSections[i].getAttribute("name"));
}

Sections.innerHTML = SelectSections.Render();

UpdateLayout();
}


function BuildLocations(sTabName, sSectionName)
{


_oXslDoc = CreateXmlDocument(true);

_oXslDoc.load("/Tools/FormEditor/Dialogs/fieldlocations.xsl");

_oXslt.stylesheet = _oXslDoc;

_oXslProc = _oXslt.createProcessor();
_oXslProc.input = _oFormXml;

_oXslProc.addParameter("tabName", sTabName);
_oXslProc.addParameter("sectionName", sSectionName);
_oXslProc.addParameter("languageCode", USER_LANGUAGE_CODE);
_oXslProc.addParameter( "Tools.FormEditor.Dialogs.fieldlocations.xsl_36", LOCID_FORMEDITOR_TAB_LABEL);
_oXslProc.addParameter( "Tools.FormEditor.Dialogs.fieldlocations.xsl_1", LOCID_FORMEDITOR_SECTION_LABEL);

_oXslProc.transform();

FieldLocations.innerHTML = _oXslProc.output;
}


function EditEventHandler()
{
if (_oEvent)
{


var oArgs = new Object();

oArgs.Event			= null;
oArgs.FormXml		= _oFormXml;
oArgs.PropertiesXml	= _oPropertiesXml;
oArgs.FieldsXml		= _oFieldsXml;
var eventIndex = -1;
var eventExists = false;
if (_oEvents)
{
var iIndex;
var iLen = _oEvents.length;

for (iIndex = 0; iIndex < iLen; iIndex++)
{
if (_oEvents[iIndex].Name.toLowerCase() == _oEvent.value.toLowerCase())
{
eventIndex = iIndex;
eventExists = true;
break;
}

}

if(eventIndex == -1)
{

eventIndex = iIndex;
}

}
else
{

_oEvents = new Array();
eventIndex = 0;
}

if(!eventExists)
{
_oEvents[eventIndex] = new EventObj(_oEvent.value.toLowerCase(), false, false, "", null);
}

oArgs.Event = _oEvents[eventIndex];

var oUpdatedEventObj = openStdDlg(prependOrgName("/Tools/FormEditor/Dialogs/event.aspx?eventName=") + encodeURI(_oEvent.value), oArgs, 550, 550, true);

if (oUpdatedEventObj)
{
_oEvents[eventIndex] = oUpdatedEventObj;
}

}
}

function SelectEventRow()
{
UnSelectEventRow();

var o = event.srcElement;

while (o.tagName != "TR")
{
o = o.parentElement;
}

o.style.backgroundColor = "#c4ddff";

_oEvent = o;
}


function UnSelectEventRow()
{
if (_oEvent)
{
var oRow = _oEvent;

oRow.style.backgroundColor = "#ffffff";
}
}

function DrawDependencyFieldSelect()
{


var oNodes		= _oFormXml.selectNodes("/entity/form/tabs/tab/sections/section/rows/row/cell/control");

var iLen		= oNodes.length;

var sNodeName	= "";

var sFieldsAry		= new Array();
sFieldsAry[0]	= new Array();
sFieldsAry[1]	= new Array();

var i = 0;

for (i = 0; i < iLen; i++)
{
sNodeName = oNodes[i].getAttribute("id");

sFieldsAry[0][i] = sNodeName;
sFieldsAry[1][i] = GetDisplayName(sNodeName);

}


Quicksort( sFieldsAry[1], 0, sFieldsAry[0].length - 1, sFieldsAry[0] );

if (_oDependencies)
{
iLen = _oDependencies.length;
}
else
{
iLen = 0;
}

var sDependsName = "";

var sDependsAry = new Array(iLen);
sDependsAry[0] = new Array(iLen);
sDependsAry[1] = new Array(iLen);

for (var i = 0; i < iLen; i++)
{
sDependsName = _oDependencies[i];

sDependsAry[0][i] = sDependsName;
sDependsAry[1][i] = GetDisplayName(sDependsName);

}



drawFieldSelect(sFieldsAry, sDependsAry);
}

function IsVariableHeightField(sDataType, sFormat)
{
return	sDataType == "memo"		||
sDataType == "iframe"	||
sDataType == "unknown"	||
sDataType == "text" && sFormat == "textarea";
}

function IsIdValid(sId)
{
sId = sId.toString();

if(sId.search(/\W/) != -1)
{
return false;
}
else
{
return true;
}
}

function GetDisplayName(sFieldName)
{
var sDisplayName = "";
var oFieldNode = _oFieldsXml.selectSingleNode("/entity/fields/field[@name='" + sFieldName + "']");
if(oFieldNode)
{
var oPropertiesNode = _oPropertiesXml.selectSingleNode("/entity/fields/field[@name='" + sFieldName + "']");
if(oPropertiesNode != null)
{
sDisplayName = oPropertiesNode.selectSingleNode("displaynames/displayname[@languagecode = " + USER_LANGUAGE_CODE + "]").getAttribute("description");
}
else
{
var sFieldLocName	= _oFieldsXml.selectSingleNode("/entity/fields/field[@name='" + sFieldName + "']").getAttribute("localizedname");
sDisplayName = ((sFieldLocName) ? sFieldLocName : sFieldName);
}
}

else
{
sDisplayName =  _oFormXml.selectSingleNode("/entity/form/tabs/tab/sections/section/rows/row/cell[control/@id = '" + sFieldName + "']/labels/label[@languagecode = " + USER_LANGUAGE_CODE + "]").getAttribute("description");
}

return sDisplayName;
}

function UpdateLayout()
{


if(typeof(crmDialog.DataType) == "undefined")
{
return;
}

var oField = window.dialogArguments.Field;
var oSection = _oFormXml.selectSingleNode("/entity/form/tabs/tab/sections/section[@name = '" + crmDialog.SectionName.value + "']");
var sDataType = crmDialog.DataType.value;
var sFormat = (oField.PropertyBag != null) ? oField.PropertyBag.Format : null;
var layout = oSection.getAttribute("layout");

if(layout == null || layout == "varwidth")
{
if(IsVariableHeightField(sDataType, sFormat))
{
crmDialog.Auto.disabled = false;
crmDialog.Auto.checked = oField.Auto;
crmDialog.Span[0].checked	= true;
crmDialog.Span[0].disabled	= true;
crmDialog.Span[1].checked	= false;
crmDialog.Span[1].disabled	= true;
}
else
{
crmDialog.Span[0].disabled	= false;
crmDialog.Span[1].disabled	= false;

if(oField.Span)
{
crmDialog.Span[0].checked = true;
}
else
{
crmDialog.Span[1].checked = true;
}

}

}

else
{
crmDialog.Span[1].checked = true;
crmDialog.Span[0].disabled	= true;
crmDialog.Span[1].disabled	= true;
if(IsVariableHeightField(sDataType, sFormat))
{
crmDialog.Auto.disabled = true;
crmDialog.Auto.checked = false;
}
}

crmDialog.Span[0].defaultChecked = crmDialog.Span[0].checked;
crmDialog.Span[1].defaultChecked = crmDialog.Span[1].checked;
}



function GetActualRowSpan()
{
var iActualRowSpan = crmDialog.NumRows.DataValue;
if(crmDialog.Auto.checked)
{

var oTab = _oFormXml.selectSingleNode("/entity/form/tabs/tab[@name = '" + crmDialog.TabName.value + "']");
var oAutoCell = oTab.selectSingleNode("sections/section/rows/row/cell[@auto = 'true' and control/@id != '" + crmDialog.FieldName.value + "' ]");

if(oAutoCell != null && oAutoCell.selectSingleNode("control").getAttribute("id") != crmDialog.FieldName.value )
{
if(confirm(LOCID_ANOTHER_EXPANDING_TAB))
{


iActualRowSpan = iActualRowSpan + 1;
}
else
{
crmDialog.Auto.checked = false;
}
}
else
{
iActualRowSpan = iActualRowSpan + 1;
}

}

return iActualRowSpan;
}


function SetUIRowSpan(oField)
{
if(oField.Auto)
{
crmDialog.NumRows.DataValue		= parseInt(oField.RowSpan,10) - 1;
}
else
{
crmDialog.NumRows.DataValue		= parseInt(oField.RowSpan,10);
}
}
